package jeumpapayment;

import jeumpapayment.record.UserData;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.XYEdges;
import net.rim.device.api.ui.component.BasicEditField;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.component.EmailAddressEditField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.PasswordEditField;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.BackgroundFactory;
import net.rim.device.api.ui.decor.Border;
import net.rim.device.api.ui.decor.BorderFactory;
//import net.rim.device.api.ui.Font;

public class RegisterScreen extends MainScreen{
	private RegisterScreen me;
	private final int WIDTH = JeumpaPayment.SCREEN_WIDTH; 
	private final int HEIGHT = JeumpaPayment.SCREEN_HEIGHT;
	
	// input field
	private BasicEditField phoneNumber;
	private PasswordEditField password;
	private EmailAddressEditField email;
	
	public RegisterScreen(){
		
		//invoke the MainScreen constructor
		super();
		this.me = this;
		VerticalFieldManager manager = (VerticalFieldManager)getMainManager();
		
		String emailInput = null;
		
//================================= Draw the background of the Registration Screen ==============================================================
		Bitmap bitmap = new Bitmap(WIDTH,HEIGHT);
		Graphics g = Graphics.create(bitmap);
		g.setColor(0x000000); //0x000000
		g.fillRect(0, 0, WIDTH,HEIGHT);
		//g.setColor(0x4B3D39);
		//g.fillRoundRect(20,70,320,100,30,30); //untuk mengambar round box (20,70,320,200,30,30)
		//g.setColor(0x777474);
		//g.drawRoundRect(20,70,320,200,30,30); // untuk mengambar round box border
		manager.setBackground(BackgroundFactory.createBitmapBackground(bitmap));
		//manager.setBackground(BackgroundFactory.createLinearGradientBackground(0xffffff, 0xffffff, 0x000000, 0x000000));
		g.drawBitmap(0,0,JeumpaPayment.BANNER.getWidth(),JeumpaPayment.BANNER.getHeight(), JeumpaPayment.BANNER,0,0);
		
//=================================================================================================================================================		
		
// ===========================perintah memasukan keterangan pengguna untuk pendaftaran ============================================================
		//create label field
		LabelField lb = new LabelField("Masukan informasi Anda untuk melakukan pendaftaran",LabelField.HCENTER){
			
			protected void paint(Graphics g){
				
				g.setColor(0xEEC900); //floralwhite
				super.paint(g);
			}
		};
		lb.setBackground(BackgroundFactory.createSolidBackground(Color.BLACK));	
		lb.setPadding(5,5,5,5); // 5 5 5 5
		lb.setMargin(45,10,30,10);
		lb.setBorder(BorderFactory.createRoundedBorder(new XYEdges(10,10,10,10),0xEEC900,Border.STYLE_SOLID)); //0x777474
		
		//add the Label field untuk perintah
		add(lb);
		
		//Phone number, pin, and email Field
		//set the background
		g.setColor(Color.BLACK); 
		g.fillRoundRect(20, 130, 280, 245, 30, 30);
		g.setColor(0xEEC900);//0xEEC900
		g.drawRoundRect(20,130,280,245,30,30); // untuk mengambar round box border
		
//==================================================PHONE NUMBER==============================================================
		
		//Phone number Field
		LabelField insertPhone = new LabelField("No Telp:"){
			
			protected void paint(Graphics g){
				
				g.setColor(0xEEC900); ////Color.FLORALWHITE
				super.paint(g);
			}
		};
		// tentukan letak untuk insert phone number
		insertPhone.setMargin(20,30,0,30);
		
		phoneNumber = new BasicEditField(EditField.FILTER_PHONE);
		phoneNumber.setBackground(BackgroundFactory.createSolidBackground(Color.WHITESMOKE));
		phoneNumber.setPadding(5,5,5,5);
		phoneNumber.setMargin(0,30,0,30);
		phoneNumber.setBorder(BorderFactory.createRoundedBorder(new XYEdges(5,5,5,5),0xFFCC00,Border.STYLE_SOLID));
		//
		
		//add phone number field and input Box
		add(insertPhone);
		add(phoneNumber);
		
//===================================================================================================================================		
		
//===================================================SECURITY NUMBER=================================================================		

		//Security Number Field
		LabelField insertSecurityNumber = new LabelField("Kata Sandi:"){
			protected void paint(Graphics g){
				g.setColor(0xEEC900);
				super.paint(g);
			}
		};
		
		insertSecurityNumber.setMargin(5,30,0,30);
		
		password = new PasswordEditField();
		password.setBackground(BackgroundFactory.createSolidBackground(Color.WHITESMOKE));
		password.setPadding(5,5,5,5);
		password.setMargin(0,30,0,30);
		password.setBorder(BorderFactory.createRoundedBorder(new XYEdges(5,5,5,5),0xFFCC00,Border.STYLE_SOLID));
		
		//add security number field and input box
		add(insertSecurityNumber);
		add(password);
		
//===================================================================================================================================		

//=================================================== EMAIL =========================================================================
		
		
		//Email Field
		LabelField insertEmail = new LabelField("Email:"){
			protected void paint(Graphics g){
				g.setColor(0xEEC900); //Color.FLORALWHITE
				super.paint(g);
			}
		};
		
		insertEmail.setMargin(5,30,0,30);
		
		email = new EmailAddressEditField("",emailInput);
		email.setBackground(BackgroundFactory.createSolidBackground(Color.WHITESMOKE));
		email.setPadding(5,5,5,5);
		email.setMargin(0,30,0,30);
		email.setBorder(BorderFactory.createRoundedBorder(new XYEdges(5,5,5,5),0xFFCC00,Border.STYLE_SOLID));
		
		//add security number field and input box
		add(insertEmail);
		add(email);
		
//======================================================================================================================================================
		
//================================================ REGISTER BUTTON =====================================================================================
		
		//button field
		
		ButtonField buttonRegister = new ButtonField("Daftar");
		buttonRegister.setMargin(10,30,30,30);
		//buttonRegister.setBackground(BackgroundFactory.createSolidBackground(0xF3E88E));
		//XYEdges bt = new XYEdges(5,5,5,5); 
		//buttonRegister.setBorder(BorderFactory.createRoundedBorder(new XYEdges(0,0,0,0),0xF3E88E,Border.STYLE_SOLID));
		//buttonRegister.setBorder(BorderFactory.createBevelBorder(bt));
		buttonRegister.setChangeListener(new RegisterButtonHandler());
		
		
		//add button to screen
		add(buttonRegister);
		
		 
		
	}
	
	public class RegisterButtonHandler implements FieldChangeListener{

		public void fieldChanged(Field field, int context) {
			// TODO Auto-generated method stub
			if(phoneNumber.getText().length()==0){
				Dialog.alert("Nomor telepon belum dimasukkan");
				return;
			}
			if(password.getText().length()==0){
				Dialog.alert("Password belum dimasukkan");
				return;
			}
			if(email.getText().length()==0){
				Dialog.alert("Email belum dimasukkan");
				return;
			}
			
			try{
				UserData userData = new UserData();
				userData.setPhonenumber(phoneNumber.getText());
				userData.setPassword(password.getText());
				userData.setEmail(email.getText());
				userData.saveRecord();
			}
			catch(Exception ex){
				ex.getMessage();
			}
			
			JeumpaPayment.switchScreen(me, new RegisterReport()) ;
		}
	}
	/*
	protected boolean onSavePrompt() {
		// TODO Auto-generated method stub
		if(exit) return super.onSavePrompt();
		return false;
	}
	*/
}
